package com.neulogistics.contrib.warehouse.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.warehouse.api.application.command.InboundCommand;
import com.neulogistics.contrib.warehouse.api.application.command.OutboundCommand;
import com.neulogistics.contrib.warehouse.api.application.command.SourceWarehouseInfoCommand;
import com.neulogistics.contrib.warehouse.api.application.query.CirculationOrderQuery;
import com.neulogistics.contrib.warehouse.api.application.query.ICirculationOrderQueryService;
import com.neulogistics.contrib.warehouse.api.application.viewmodel.CirculationOrderViewModel;
import com.neulogistics.contrib.warehouse.api.application.viewmodel.WarehouseViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "出入库单管理")
@RequestMapping("/warehouse/circulationOrder")
public class CirculationOrderController {
    @Resource
    IMediator mediator;

    @Resource
    private ICirculationOrderQueryService circulationOrderQueryService;

    @ApiOperation("出库")
    @PostMapping("/outbound")
    public BaseResponse<Boolean> outbound(
            @RequestBody @Validated @NotNull OutboundCommand command){
        // 已校验数据格式

        return mediator.send(command);
    }

    @ApiOperation("入库")
    @PostMapping("/inbound")
    public BaseResponse<Boolean> inbound(
            @RequestBody @Validated @NotNull InboundCommand command){
        // 已校验数据格式
        BaseResponse<Boolean> baseResponse = mediator.send(command);
        return baseResponse == null ?
                ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public BaseResponse<CirculationOrderViewModel> get(@PathVariable(value = "id") @NotNull @Positive Long id){
        // 已校验数据格式
        CirculationOrderViewModel circulationOrderViewModel = circulationOrderQueryService.getById(id);

        return ResultUtils.success(circulationOrderViewModel);
    }

    @ApiOperation("分页查询调度单")
    @PostMapping("/page")
    public BaseResponse<Page<CirculationOrderViewModel>> page(@RequestBody @Validated @NotNull CirculationOrderQuery circulationOrderQuery){

        Page<CirculationOrderViewModel> viewModelPage = circulationOrderQueryService.page(circulationOrderQuery);

        return ResultUtils.success(viewModelPage);
    }

    @ApiOperation("查询来源库房信息")
    @PostMapping("/getSourceWarehouseInfo")
    public BaseResponse<WarehouseViewModel> getSourceWarehouseInfo(@RequestBody @NotNull SourceWarehouseInfoCommand command){

        BaseResponse<WarehouseViewModel> baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }
}
